/*
 * Authors: Alex Attar & Kevin El Hage
 * ID: 9765727
 * Date: 9/3/2012
 * 
 */

package taskManagerSystem.ViewSubsystem;

import javax.swing.JTable;
import javax.swing.event.TableModelListener;

/*
 * The Table class is a JTable used to display the tasks and people.
 */
public class Table extends JTable {
	private static final long serialVersionUID = 1L;
	private TableMod tableModel;
	private String[] colNames;
	private Class<?>[] colTypes;
	private boolean[] editCols;
	
	public Table(String[] columnNames, Class<?>[] columnTypes){
		colNames = columnNames; 
		colTypes = columnTypes;
		editCols = new boolean[colNames.length];
		for(int i=0; i!=editCols.length; i++){
			editCols[i] = true;
		}
		tableModel = new TableMod(colNames, colTypes);
		setModel(tableModel);
	}
	
	public void addRow(){
		tableModel.addRow();
	}
	
	public void addRow(String[] data){
		tableModel.addRow(data);
	}
	
	public void deleteRow(int index){
		tableModel.deleteRow(index);
	}
	
	public String[] getRow(int rowNum){
		String[] row = new String[colNames.length];
		if (rowNum >= 0 && rowNum < tableModel.getRowCount()){
			for(int i=0; i!=colNames.length; i++){
				row[i] = String.valueOf(tableModel.getValueAt(rowNum, i));
			}
		}
		return row;
	}
	
	public void addTableListener(TableModelListener l){
		tableModel.addTableModelListener(l);
	}
	
	public void empty(){
		tableModel.empty();
	}
	
	public void update(){
		System.out.println("Updating");
		tableModel.fireTableDataChanged();
	}
	
	@Override
	public boolean isCellEditable(int row, int column){
		return editCols[column];
	}
	
	public void disableColumn(int index){
		editCols[index] = false;
	}
	
}
